package Snapshot;

import Bean.MindMapTemp;

import java.util.HashMap;
import java.util.Stack;

public class SnapshotStack {

    private static HashMap<Integer, Stack<MindMapTemp>> snapshotsOfReback = new HashMap<>();
    private static HashMap<Integer, Stack<MindMapTemp>> snapshotsOfNext = new HashMap<>();

    public static void storeSnapshotOfReback(Integer id,MindMapTemp temp){
        Stack<MindMapTemp> stack = snapshotsOfReback.get(id);
        if(stack==null){
            stack = new Stack<>();
            snapshotsOfReback.put(id,stack);
        }
        stack.push(temp);
    }


    public static MindMapTemp getSnapshotOfReback(Integer id){
        if(snapshotsOfReback.get(id).isEmpty()){
            return null;
        }
        if(snapshotsOfReback.get(id).size()==1){
            MindMapTemp temp = snapshotsOfReback.get(id).peek();
            return temp;
        }
        if(snapshotsOfNext.get(id)==null){
            snapshotsOfNext.put(id,new Stack<>());
        }
        snapshotsOfNext.get(id).push(snapshotsOfReback.get(id).pop());
        MindMapTemp temp = snapshotsOfReback.get(id).pop();
        snapshotsOfReback.get(id).push(temp);
        return temp;
    }

    public static MindMapTemp getSnapshotOfNext(Integer id){
        if(snapshotsOfNext.get(id)==null||snapshotsOfNext.get(id).isEmpty()){
            return null;
        }
       MindMapTemp temp = snapshotsOfNext.get(id).pop();
        return temp;
    }

}
